/**
 * @author ccpalu
 */
	
 function cnvMap(behest, sampleID, dist){

	var cnv = document.getElementById('CNV');
	var ch = cnv.ch;
	var graphicVar= document.getElementById('graphicVariables');
	
	var chrSamples;
	var sample;
	
	var chmetrics=chromossomeMetrics();
	remove_element_by_id("simpleChart");
	/* position settings */
	var h = graphicVar.generalHeight(); //document.getElementById("simplechart").style.height.replace('px',"")*1-52;
	var t = graphicVar.top;//document.getElementById("simplechart").style.top.replace('px',"")*1+10;
	var l = graphicVar.CNVLeft();//document.getElementById("chartLabel").style.left.replace('px',"")*1+28;
	var w = graphicVar.CNVWidth();//document.getElementById("simplechart").style.left.replace('px',"")*1 - l + 18 ;
	var segH = graphicVar.segmentHeight;
	var max, min, limit=0;
	var segW = ((w - (ch.length-1)*2)/chmetrics.total());
	
	var xi, wid,yi;
	//Chromosome size legends
	remove_element_by_id('legends');
	legends= document.createElement('OBJECT');
	legends.id='legends';
	document.getElementById('chartingArea').appendChild(legends);
	//Adding chromosome pictures
	for (var h = 0; h < ch.length; h = h + 1) {
		min = cnv.min[ch[h]];
		max = cnv.max[ch[h]];
		
		chImage(ch[h], (max-min)*segW, l + limit*segW);
		
		//Adding labels to this chromosome
		var mean = (max - min) / 2;
		var addTop=76;
		var specialSpace=0;
		if (((max - min) * segW) < 78) 
			 specialSpace=2;
		
		setPosition(createParagraph("LegendCh" + ch[h]+"BeginningMark", "|", legends), (1+l+(limit * segW) -1), 263+addTop+15);
		setFont("LegendCh" + ch[h]+"BeginningMark", "10px");
		setPosition(createParagraph("LegendCh" + ch[h]+"BeginningNum", numberComma(Math.round(min/10000)/100), legends), (l+3+(limit*segW)), 252 + addTop - specialSpace+15);
		setFont("LegendCh" + ch[h]+"BeginningNum", "10px",0,0);
		document.getElementById("LegendCh" + ch[h]+"BeginningNum").style.letterSpacing="-0.5px";

		setPosition(createParagraph("LegendCh" + ch[h], ch[h],legends), (l+(mean + limit)*segW), 205 + addTop+15);
		setFont("LegendCh" + ch[h], "13px",0,0,'bolder');
		
		setPosition(createParagraph("LegendCh" + ch[h]+"EndMark", "|", legends), (l-1+(max - min + limit )*segW), 263 + addTop+15);
		setFont("LegendCh" + ch[h]+"EndMark", "10px");
		setPosition(createParagraph("LegendCh" + ch[h]+"EndNum", numberComma(Math.round(max/10000)/100),legends), (l-33+(2*specialSpace)+(max - min + limit)*segW), 252 + addTop +(3*specialSpace)+15);
		setFont("LegendCh" + ch[h]+"EndNum", "10px");
		document.getElementById("LegendCh" + ch[h]+"EndNum").style.letterSpacing="-0.5px";
		
		limit = limit+chmetrics[ch[h]]+(2/segW);
	}
	//Distance Plot
	var divCanvas=	document.createElement('DIV');
	setPosition(divCanvas,l,t);
	divCanvas.id = 'copyNumber';
	document.body.appendChild(divCanvas);
	
	var divDist = document.createElement('DIV');
	setPosition(divDist,graphicVar.distanceLeft(), graphicVar.top);
	divDist.id = 'simpleChart';
	document.getElementById('chartingArea').appendChild(divDist);
	divDist.style.width = graphicVar.distanceWidth+'px';
	//divDist.style.zIndex=-20;
	
	var canvas, distCanvas;
	if (document.getElementById("sim_method").value!="0")
		var barRate = divDist.style.width.replace("px","") / dist[dist.length-1];  
	limit=0;
	for (var i = 0; i < cnv.segData[ch[0]].length; i = i + 1) {
		
		var label = sampleID[i];
		
		canvas=	createCanvas(w,segH, 'cnv' + label, divCanvas);
		setPosition(canvas,0,segH * i);
		canvas.style.backgroundColor='#FFFFFF';
		canvas.title ="Sample ID: " + label;
		
		var ctx = canvas.getContext("2d"), ctxD;
		
		//Distance chart bars
		if (document.getElementById("sim_method").value!="0"){
			distCanvas=	createCanvas(graphicVar.distanceWidth,segH, 'cnv' + label, divDist);
			setPosition(distCanvas,0,segH * i);
			distCanvas.style.backgroundColor='#CCFFFF';
			ctxD = distCanvas.getContext("2d");
			if (i)
				distCanvas.title ="Sample ID: " + label + " Distance: "+ ((Math.round(dist[i] * 100)) / 100);
			else
				distCanvas.title ="Sample ID: " + label + " Target Sample";
			
			ctxD.fillStyle = '#6699CC';
			ctxD.fillRect(0, 0.1, barRate * dist[i], segH-0.2);
			ctxD.strokeStyle = '#6666CC';
			ctx.strokeRect(0, 0.1, barRate * dist[i], segH-0.2);  
		}
		//CNV heatmap
		for (var h = 0; h < ch.length; h = h + 1) {
			chrSamples=cnv.segData[ch[h]];
			sample = chrSamples[behest[i]];
			
			min = cnv.min[ch[h]];
			max = cnv.max[ch[h]];	
				
			for (var j = 0; j < sample['xi'].length; j = j + 1) {
				//if (j<10)
					//var t= 0;
				xi = (sample['xi'][j] - min + limit) * segW;
				wid = (sample['xf'][j] - sample['xi'][j]) * segW;
				yi = 0.05;
										
				ctx.fillStyle = sample['color'][j];
				ctx.fillRect(xi, yi, wid, segH-0.1);
			}
			limit = limit+chmetrics[ch[h]]+(2/segW);
		}
		limit=0;
		
		}
	
	try{
		document.getElementById('chartTitleDiv').style.width = w+'px';
		plotPsub=document.getElementById("plotPsub");
		if (document.getElementById("sim_method").selectedIndex){//Abril 2011
			plotPsub.innerHTML="- Sorted by " + document.getElementById("sim_method").options[document.getElementById("sim_method").selectedIndex].text+" distance -";
		}
		else{	plotPsub.innerHTML="- Copy Number data overview -";}
			
		
	}
	catch(err){
		var chartTitleDiv = document.createElement('DIV');
		chartTitleDiv.id = 'chartTitleDiv';
		chartTitleDiv.style.width = w+'px';
		chartTitleDiv.style.zIndex=-10;
		setPosition(chartTitleDiv, l, t-265+addTop+15);
		document.body.appendChild(chartTitleDiv);

		var plotP=createParagraph("plotP", document.getElementById('chartTitle').value, chartTitleDiv, 'center');

		setFont(plotP,'18px','italic');
		
		var plotPsub=createParagraph("plotPsub", "- Copy Number data overview -", chartTitleDiv, 'center');
		if (window.navigator.appName == 'Netscape') {
			plotPsub.style.top='30px';
		}
		else{
			plotPsub.style.top='45px';// l+(w/2)-plotPsub.innerHTML.length*4.2, t-235+addTop+15);
		}
		setFont(plotPsub,'16px','italic');
		plotPsub.style.fontWeight='300';

		var exponential="7";
		var plotPlegend=createParagraph("plotPlegend", "Heatmap of CNV separated by chromosomes (x 10"+exponential.sup()+" base pairs)");
		setPosition(plotPlegend, l+10,t-105+15);
		setFont(plotPlegend,'12px');

		plotP.style.zIndex=-6;
		plotPsub.style.zIndex=-7;
		plotPlegend.style.zIndex=-8;
	}

	remove_element_by_id("LoadingStatus");
	if (document.getElementById("canvasHB")==null)
		heatBar();
	
	return false;
}

function chImage(ch, w, l){
/* Displays the ch image above CNV heatmap marking the selected region*/
	var cnv = document.getElementById("CNV");
	if ((window.navigator.appName == 'Netscape') || (window.navigator.appName == 'Opera'))
		var t= document.getElementById('graphicVariables').top-45;
	else
		var t= document.getElementById('graphicVariables').top-30;

	if (ch<10)
		var id="0"+ch;
	else
		var id= ch;
	
	var parent=document.getElementById("chrefDiv");
	if (!parent){
		var parent = document.createElement('DIV');
		parent.id = "chrefDiv";
		document.getElementById('chartingArea').appendChild(parent);
	}
	
	var containner = document.createElement('DIV');
	containner.id = "ch"+id+"refDiv";
	parent.appendChild(containner);
	containner.style.zIndex=-15;
	
	var imgNode = document.createElement('DIV');
	imgNode.id = "ch"+id+"refDivImg";
	imgNode.innerHTML = '<img id="ch' + id + 'refImg" src="http://cnviewer.googlecode.com/hg/Images/ch' + id + '.png"\>';
	containner.appendChild(imgNode);
	dojo.attr(imgNode,"style",{left:"0", top:"0", position:"absolute"});//, overflow:"hidden"});20/09/2010

	var img = document.getElementById("ch"+id+"refImg");
	img.width=w;
	img.height=15;

	dojo.attr(containner,"style",{width:w,height:45,left:l + "px", top: t + "px", position:"absolute"});//21/09/2010, overflow:"hidden"});
	
	var node1 = document.createElement("div");
    containner.appendChild(node1);
    
	dojo.attr(node1,"style",{left:"0", top:"0", height:75, width:w+50, position:"relative"});
	var surface = dojox.gfx.createSurface(node1,img.width+50, 75);
	surface.getEventSource().setAttribute('id','ch'+id+'surface');
	
	for (var i=0; i < (cnv.chRegions[ch].length / 2); i = i + 1) {
		locateChFragment(ch, (i+1), surface);
	}
	return false;
}

function locateChFragment(ch, fragment, surface){	
	fragment=(fragment-1)*2;
	if (ch < 10) {
		var w = document.getElementById("ch0" + ch + "refImg").width;
	}
	else {
		var w = document.getElementById("ch"+ch+"refImg").width;
	}
	var cnv=document.getElementById("CNV");
	var img = new Array();
	img.rate = w / cnv.realSize[ch];
	var canv = new Array();
	canv.rate = w/(cnv.max[ch]-cnv.min[ch]);
	canv.start = cnv.min[ch];// *canv.rate;
	canv.end = cnv.max[ch];// *canv.rate;

	img.start = cnv.chRegions[ch][fragment] * img.rate;
	img.end = cnv.chRegions[ch][fragment + 1] * img.rate;
	canv.end = (cnv.chRegions[ch][fragment+1] - canv.start)* canv.rate;// * canv.rate - canv.start;
	canv.start = (cnv.chRegions[ch][fragment] - canv.start)* canv.rate;// * canv.rate - canv.start;

	canv.width = canv.end - canv.start;	
	img.width = img.end - img.start;
	
	
	surface	//		Beginning @ ChPicture 				  Goes down the same height of ChPic
		.createPath("m"+img.start+" 2 " + img.width + " 0 0 11 " + (canv.end-img.end) +" 32 "+ (-1*canv.width) + " 0 "+(img.start-canv.start)+" -32 0 -11")
		.setFill("rgba(60,150,165,0.3)")//.setFill("rgba(30,180,200,0.4)")
		.setTransform({dx: 0, dy: 0});

	return false;
}

function heatBar(){
// It creates a bar containing the values correspondent to the colors on the Heatmap
//
	var cnv=document.getElementById('CNV');
	var HB = document.createElement('OBJECT');
	HB.id='HB';
	document.body.appendChild(HB);

	var w = 180, h=30;
	
	setPosition(HB, 20, document.getElementById('Parameters').style.height.replace("px","")*1+ 335);//April 2011 305);
	HB.style.width=1*(w+4)+"px";
	setFont(createParagraph("HBp","Copy Number", HB),'13px',0,'center');
	
	var canvas = createCanvas(w, h,'canvasHB',HB);
	canvas.style.left = "2px";
	
	var ctx = canvas.getContext("2d");
	var hbValues=[0,1,2,3,4,52,100];
	var rate=1/(hbValues.length-1);
	var interval = (w-1)*rate;
	for (var j = 0; j < (w*4/6); j = j + 1) {
			ctx.fillStyle = heatMapHexa(j/(w/6));
			ctx.fillRect(j, 0, 1, h);
	}
	for (j=0; j < w/3; j = j + 1) {
			ctx.fillStyle = heatMapHexa((96/(w/3)*j)+4);
			ctx.fillRect(j+120, 0, 1, h);
	}
	ctx.strokeRect(0,0,180,30);
				
	HB.legends=new Array();
	var positionCorrection = 9;
	var topStart=0;
	if (window.navigator.vendor == "Apple Computer, Inc.") 
		topStart = 1;
	
		
	for (var i = 0; i < hbValues.length; i = i + 1) {
		HB.legends["l"+i] = createParagraph("hbl"+i, "|<br>"+hbValues[i],HB);
		setFont(HB.legends["l"+i], "9px");
		HB.legends["l"+i].style.height="0px";
		setPosition(HB.legends["l"+i], i*interval-1, ((i*-positionCorrection)-topStart), 'relative');
		HB.legends["l"+i].style.zIndex=-5;
	}
	

	return false;
}


